/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.controller.GopalaMithraCtlr;
import org.gov.vis.master.controller.GopalaMithraCtlrImpl;
import org.gov.vis.master.dto.GopalaMithraDto;
import org.gov.vis.master.gui.AddGopalMithraDialog;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author Vishwanath
 *
 */
public class AddGopalaMithraListener implements ActionListener,BaseListener{

	private AddGopalMithraDialog gmithraDailog;
	private GopalaMithraCtlr gmCtlr = new GopalaMithraCtlrImpl();
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private GopalaMithraDto gmDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public AddGopalaMithraListener(AddGopalMithraDialog othersDialog){

		this.gmithraDailog = othersDialog;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(gmithraDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the GopalaMithra details
				addOrEditGopalaMithra();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){	
				//Method called to view the details of GopalaMithra Object based on the passed gopalaMithraId
				viewGopalaMithra(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the GopalaMithra Object based on the passed gopalaMithraId.
				deleteGopalaMithra(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}//end actionPerformed

	private void viewGopalaMithra(final long _gopalaMithraId) throws Exception {
		// TODO Auto-generated method stub
		final GopalaMithraDto _gmDto = gmCtlr.getGopalaMithra(_gopalaMithraId);
		setDtoDetailsToGui(_gmDto);
		gmithraDailog.validate();
		gmithraDailog.repaint();

	}

	/**
	 * Method called to delete the GopalaMithra Object based on the passed gopalaMithraId
	 * @param _gopalaMithraId
	 */
	private void deleteGopalaMithra(final long  _gopalaMithraId){
		try{
			gmCtlr.deleteGopalaMithra(_gopalaMithraId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteGopalaMithra

	/**
	 * This creates a JPanel with the given success message.
	 * 
	 * @param msg
	 * @return JPanel
	 */
	public static JPanel getSuccessMsgPanel(String msg) {
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 0));
		JLabel label = new JLabel(msg);
		label.setForeground(Color.GREEN);
		panel.add(label);
		return panel;
	}


	/**
	 * Method called to either Add or Edit the GopalaMithra details
	 */
	private void addOrEditGopalaMithra(){

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);


		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(gmithraDailog);

		try{
			//Method called to Add or Edit the GopalaMithra
			gmCtlr.addOrEditGopalaMithra(gmDto);

			/*List<String> successMsgLst = new ArrayList<String>();
			successMsgLst.add(rb.getString(GuiConstants.MASTER_ADD_GOPALAMTHRA_SUCCESS));
			errorMsgPanel.setSuccessMessages(successMsgLst);
			*/
			//otherCtlr.addOrEditOther(otherDto);
			JOptionPane.showMessageDialog(gmithraDailog, rb.getString(GuiConstants.MASTER_ADD_GOPALAMTHRA_SUCCESS));
			//gmithraDailog.dispose();	


			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = GopalaMithraCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;
					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_code").toUpperCase())){
						gmithraDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_name").toUpperCase())){
						gmithraDailog.getLblName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_name").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_fathername").toUpperCase())){
						gmithraDailog.getLblFatherName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_fathername").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_dob").toUpperCase())){
						gmithraDailog.getLblDateOfBirth().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_dob").toUpperCase());
						errorMsgLst.add(errMsg);
					}					
					
					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}		
		}
	}//end addOrEditGopalaMithra

	/**
	 * Method called to Clear all the form fields of the screen
	 */
	@Override
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		gmithraDailog.getTxtbxCode().setText("");
		gmithraDailog.getTxtbxName().setText("");
		gmithraDailog.getFatherTxtBx().setText("");
		gmithraDailog.getJurisdiction().setText("");
		gmithraDailog.getHeadQuarterTxtBx().setText("");
		gmithraDailog.getMandalTxtBx().setText("");
		gmithraDailog.getDistrictTxtBx().setText("");
		gmithraDailog.getDtChoDateOfBirth().setDate(null);
		gmithraDailog.getNativeVillageTxtBx().setText("");
		gmithraDailog.getJudMandalTxtBx().setText("");
		gmithraDailog.getJTextField1().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);



	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		gmDto = (GopalaMithraDto)object;
		gmithraDailog.getTxtbxCode().setText(gmDto.getCode());
		gmithraDailog.getTxtbxName().setText(gmDto.getName());
		gmithraDailog.getFatherTxtBx().setText(gmDto.getFatherName());
		gmithraDailog.getJurisdiction().setText(gmDto.getJurisdiction());
		gmithraDailog.getHeadQuarterTxtBx().setText(gmDto.getHeadQuarter());
		gmithraDailog.getMandalTxtBx().setText(gmDto.getMandal());
		gmithraDailog.getDistrictTxtBx().setText(gmDto.getDistrict());
		gmithraDailog.getDobTxtBx().setText(DU.format(gmDto.getDob(),DBConstants.DISPLAY_DATE_FORMAT));
		gmithraDailog.getNativeVillageTxtBx().setText(gmDto.getNativeVillage());
		gmithraDailog.getJudMandalTxtBx().setText(gmDto.getHqMandal());
		gmithraDailog.getJudDistrict().setText(gmDto.getHqDistrict());

	}

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		AddGopalMithraDialog _gmithraDailog = (AddGopalMithraDialog)object; 
		gmDto = new GopalaMithraDto();
		gmDto.setCode(_gmithraDailog.getTxtbxCode().getText());
		gmDto.setName(_gmithraDailog.getTxtbxName().getText());
		gmDto.setFatherName(_gmithraDailog.getFatherTxtBx().getText());
		/*if(_gmithraDailog.getDobTxtBx().getText() != null && !_gmithraDailog.getDobTxtBx().getText().equals("")){
			gmDto.setDob(DU.parse(_gmithraDailog.getDobTxtBx().getText(), DBConstants.DISPLAY_DATE_FORMAT));
		}*/
		if(_gmithraDailog.getDtChoDateOfBirth().getDate() != null){
			gmDto.setDob(_gmithraDailog.getDtChoDateOfBirth().getDate());
			}

		gmDto.setNativeVillage(_gmithraDailog.getNativeVillageTxtBx().getText());
		gmDto.setMandal(_gmithraDailog.getMandalTxtBx().getText());
		gmDto.setDistrict(_gmithraDailog.getDistrictTxtBx().getText());
		gmDto.setJurisdiction(_gmithraDailog.getJurisdiction().getText());
		gmDto.setHeadQuarter(_gmithraDailog.getHeadQuarterTxtBx().getText());
		gmDto.setHqDistrict(_gmithraDailog.getJTextField1().getText());
		gmDto.setHqMandal(_gmithraDailog.getJudMandalTxtBx().getText());

	}

	public void setGopalMithrasDailog(final AddGopalMithraDialog _GMDailog) {
		this.gmithraDailog = _GMDailog;
	}

	public AddGopalMithraDialog getGopalaMithrasDailog() {
		return gmithraDailog;
	}

	public void setGopalaMithraCtlr(final GopalaMithraCtlr _gMCtlr) {
		this.gmCtlr = _gMCtlr;
	}

	public GopalaMithraCtlr getGopalaMithraCtlr() {
		return gmCtlr;
	}

	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		gmithraDailog.getLblCode().setForeground(_lblColor);
		gmithraDailog.getLblName().setForeground(_lblColor);
		gmithraDailog.getLblDistrict().setForeground(_lblColor);
		gmithraDailog.getLblHeadQuarter().setForeground(_lblColor);
		gmithraDailog.getLblJurisdiction().setForeground(_lblColor);
		gmithraDailog.getLblMandal().setForeground(_lblColor);
		gmithraDailog.getLblDateOfBirth().setForeground(_lblColor);
		gmithraDailog.getLblHQDistrict().setForeground(_lblColor);
		gmithraDailog.getLblHQMandal().setForeground(_lblColor);
		gmithraDailog.getLblFatherName().setForeground(_lblColor);
		gmithraDailog.getLblNativeVillage().setForeground(_lblColor);


	}

}
